home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / uim / uim-sh.scm < prev    next >
Encoding:
Text File  |  2010-11-07  |  6.2 KB  |  187 lines

  1. ;;; uim-sh.scm: uim interactive shell for debugging, batch processing
  2. ;;;             and serving as generic inferior process
  3. ;;;
  4. ;;; Copyright (c) 2003-2009 uim Project http://code.google.com/p/uim/
  5. ;;;
  6. ;;; All rights reserved.
  7. ;;;
  8. ;;; Redistribution and use in source and binary forms, with or without
  9. ;;; modification, are permitted provided that the following conditions
  10. ;;; are met:
  11. ;;; 1. Redistributions of source code must retain the above copyright
  12. ;;;    notice, this list of conditions and the following disclaimer.
  13. ;;; 2. Redistributions in binary form must reproduce the above copyright
  14. ;;;    notice, this list of conditions and the following disclaimer in the
  15. ;;;    documentation and/or other materials provided with the distribution.
  16. ;;; 3. Neither the name of authors nor the names of its contributors
  17. ;;;    may be used to endorse or promote products derived from this software
  18. ;;;    without specific prior written permission.
  19. ;;;
  20. ;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
  21. ;;; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. ;;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. ;;; ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE
  24. ;;; FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26. ;;; OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. ;;; HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28. ;;; LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29. ;;; OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. ;;; SUCH DAMAGE.
  31. ;;;;
  32.  
  33. (require-extension (srfi 1 2 6 23 34 48))
  34.  
  35. (define %HYPHEN-SYM (string->symbol "-"))
  36.  
  37. (define uim-sh-prompt "uim> ")
  38. (define uim-sh-opt-expression #f)
  39. (define uim-sh-opt-arg-expression
  40.   "(error \"no <expr> is passed as argument\")")
  41.  
  42. (define uim-sh-option-table
  43.   `((("-b" "--batch")          . batch)
  44.     (("-B" "--strict-batch")   . strict-batch)
  45.     (("-r" "--require-module") . ,(lambda (args)
  46.                     (and-let* ((name (safe-car args))
  47.                            ((require-module name)))
  48.                       (safe-cdr args))))
  49.     (("--editline")            . ,(lambda (args)
  50.                     (require-module "editline")
  51.                     args))
  52.     (("-e" "--expression")     . ,(lambda (args)
  53.                     (set! uim-sh-opt-expression #t)
  54.                     (and-let* ((expr (safe-car args)))
  55.                       (set! uim-sh-opt-arg-expression expr)
  56.                       (safe-cdr args))))
  57.     (("-V" "--version")        . version)
  58.     (("-h" "--help")           . help)))
  59.  
  60. (define uim-sh-usage
  61.   (lambda ()
  62.     (display "Usage: uim-sh [options] [file [arg ...]]
  63.   -b
  64.   --batch                 batch mode. suppress shell prompts
  65.   -B
  66.   --strict-batch          strict batch mode, implies -b. suppress shell prompts
  67.                           and evaluated results
  68.   -r <name>
  69.   --require-module <name> require module
  70.   --editline              require editline module for Emacs-like line editing
  71.   -e <expr>
  72.   --expression <expr>     evaluate <expr> (after loading the file, and disables
  73.                           'main' procedure of it)
  74.   -V
  75.   --version               show software version
  76.   -h
  77.   --help                  show this help
  78.   file                    absolute path or relative to system scm directory
  79.   arg ...                 string argument(s) for 'main' procedure of the file
  80. ")))
  81.  
  82. (define uim-sh-display-version
  83.   (lambda ()
  84.     (format #t "uim-sh ~a [SigScheme ~a]" (uim-version) (sscm-version))
  85.     (newline)))
  86.  
  87. (define uim-sh-define-opt-vars
  88.   (lambda (opt-table prefix)
  89.     (for-each (lambda (name)
  90.         (let ((sym (symbol-append prefix %HYPHEN-SYM 'opt- name)))
  91.           (eval `(define ,sym #f)
  92.             (interaction-environment))))
  93.           (filter symbol? (map cdr opt-table)))))
  94.  
  95. (define uim-sh-parse-args
  96.   (lambda (opt-table prefix args)
  97.     (uim-sh-define-opt-vars opt-table prefix)
  98.     (let rec ((args args))
  99.       (or (and-let* (((pair? args))
  100.              (opt (car args))
  101.              (rest (cdr args))
  102.              (ent (assoc opt opt-table member))
  103.              (action (cdr ent)))
  104.         (cond
  105.          ((symbol? action)
  106.           (set-symbol-value!
  107.            (symbol-append prefix %HYPHEN-SYM 'opt- action) #t))
  108.          ((procedure? action)
  109.           (set! rest (action rest)))
  110.          (else
  111.           (error "invalid action on option table")))
  112.         (rec rest))
  113.       args
  114.       '()))))  ;; an action possibly returns #f
  115.  
  116. (define uim-sh-loop
  117.   (lambda (my-read)
  118.     (if (and (not uim-sh-opt-batch)
  119.          (not uim-sh-opt-strict-batch)
  120.          (not (provided? "editline")))
  121.     (display uim-sh-prompt))
  122.     ;; Non-recoverable read error is turned into fatal errorr such as
  123.     ;; non-ASCII char in token on a non-Unicode port.
  124.     (let ((expr (guard (read-err
  125.             (else (%%fatal-error read-err)))
  126.           (my-read))))
  127.       (and (not (eof-object? expr))
  128.        (let ((res (eval expr (interaction-environment))))
  129.          (if (not uim-sh-opt-strict-batch)
  130.          (writeln res))
  131.          (uim-sh-loop my-read))))))
  132.  
  133. ;; Loop even if error has been occurred. This is required to run
  134. ;; GaUnit-based unit test for uim.
  135. (define uim-sh
  136.   (lambda (args)
  137.     (let* ((file.args (uim-sh-parse-args uim-sh-option-table
  138.                      'uim-sh
  139.                      (cdr args))) ;; drop the command name
  140.        (script (safe-car file.args))
  141.        (my-read (if (provided? "editline")
  142.             (begin
  143.               (set! *editline-prompt-beginning* uim-sh-prompt)
  144.               editline-read)
  145.             read))
  146.        (EX_OK       0)
  147.        (EX_SOFTWARE 70))
  148.       (cond
  149.        (uim-sh-opt-help
  150.     (uim-sh-usage)
  151.     EX_OK)
  152.  
  153.        (uim-sh-opt-version
  154.     (uim-sh-display-version)
  155.     EX_OK)
  156.  
  157.        (uim-sh-opt-expression
  158.     (let* ((expr (read (open-input-string uim-sh-opt-arg-expression)))
  159.            (result (eval expr (interaction-environment))))
  160.       (if (not uim-sh-opt-strict-batch)
  161.           (begin
  162.         (write result)
  163.         (newline)))
  164.       EX_OK))
  165.  
  166.        (script
  167.     (require script)
  168.     (if (symbol-bound? 'main)
  169.         (let ((status (main file.args)))
  170.           (if (integer? status)
  171.           status
  172.           EX_SOFTWARE))
  173.         EX_OK))
  174.  
  175.        (else
  176.     (let reloop ()
  177.       (and (guard (err (else
  178.                 (%%inspect-error err)
  179.                 #t))
  180.          (uim-sh-loop my-read))
  181.            (reloop)))
  182.     EX_OK)))))
  183.  
  184. ;; Verbose level must be greater than or equal to 1 to print anything.
  185. (if (< (verbose) 1)
  186.     (verbose 1))
  187.